﻿using Abp.Dependency;
using RabbitMQ.GNClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Core.Tasks.Queue.Impl
{
    class QueueManager : IQueueManager
    {
        private readonly IIocManager iocManager;

        public QueueManager(IIocManager iocManager)
        {
            this.iocManager = iocManager;
        }

        /// <summary>
        /// 获取队列
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IProduceQueue GetProduceQueue(string name)
        {
            return iocManager.Resolve<IProduceQueue>(new { name });
        }

        /// <summary>
        /// 获取队列
        /// </summary>
        /// <param name="cacheType">类型</param>
        /// <returns></returns>
        public IProduceQueue GetProduceQueue(QueueType cacheType)
        {
            return GetProduceQueue(cacheType.ToString());
        }

        /// <summary>
        /// 发布消息
        /// </summary>
        /// <param name="queueType">类型</param>
        /// <param name="message">消息</param>
        /// <param name="delayConsumer">过期</param>
        public void Publish(QueueType queueType, ObjectMessage message, TimeSpan? delayConsumer = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            using (var queue = GetProduceQueue(queueType))
            {
                queue.Publish(message, delayConsumer);
            }
        }

        /// <summary>
        /// 异步发布消息
        /// </summary>
        /// <param name="queueType">类型</param>
        /// <param name="message">消息</param>
        /// <param name="delayConsumer">过期</param>
        public async Task PublishAsync(QueueType queueType, ObjectMessage message, TimeSpan? delayConsumer = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            using (var queue = GetProduceQueue(queueType))
            {
                await queue.PublishAsync(message, delayConsumer);
            }
        }

        /// <summary>
        /// 发布消息
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="message">消息</param>
        /// <param name="delayConsumer">过期</param>
        public void Publish(string name, ObjectMessage message, TimeSpan? delayConsumer = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            using (var queue = GetProduceQueue(name))
            {
                queue.Publish(message, delayConsumer);
            }
        }

        /// <summary>
        /// 异步发布消息
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="message">消息</param>
        /// <param name="delayConsumer">过期</param>
        public async Task PublishAsync(string name, ObjectMessage message, TimeSpan? delayConsumer = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            using (var queue = GetProduceQueue(name))
            {
                await queue.PublishAsync(message, delayConsumer);
            }
        }
    }
}
